package com.gxgeek.javabasic.Thread;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author gaoxing
 * @create 2018-06-02 上午11:47
 */
public class TransmittableThreadLocalTest {
    public static void main(String[] args)  throws Exception{
        final ThreadLocal<InheritableThreadLocalTest.Person> threadLocal=new TransmittableThreadLocal<>();
        threadLocal.set(new InheritableThreadLocalTest.Person("Java架构沉思录"));
        System.out.println("初始值："+threadLocal.get());
        Runnable task=()->{
            System.out.println("----------start------------");
            System.out.println("父线程的值："+threadLocal.get());
            threadLocal.set(new InheritableThreadLocalTest.Person("沉思君"));
            System.out.println("子线程覆盖后的值："+threadLocal.get());
            System.out.println("------------end---------------");
        };
        ExecutorService executorService= Executors.newFixedThreadPool(1);
        Runnable runnable= TtlRunnable.get(task);
        executorService.submit(runnable);
        TimeUnit.SECONDS.sleep(1);
        executorService.submit(runnable);
        TimeUnit.SECONDS.sleep(1);
        executorService.submit(runnable);
    }

}
